<template>
    <el-container>
        <el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				 <el-button type="primary" icon="el-icon-circle-close" @click="batchCancel">取消收藏</el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="企业名或统一社会信用代码"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="companyId" @selection-change="selectionChange" stripe remoteSort remoteFilter>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="统一社会信用代码" prop="unifiedCode" width="180" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="企业名称" prop="name" width="320"  :show-overflow-tooltip="true"></el-table-column>
				<!-- <el-table-column label="注册地址" prop="reg_area" :show-overflow-tooltip="true" width="120"></el-table-column> -->
				<el-table-column label="企业性质" prop="aptitude" :show-overflow-tooltip="true" width="260"></el-table-column>
				<!--<el-table-column label="人员规模" prop="person_num" :show-overflow-tooltip="true" width="100"></el-table-column>-->
				<el-table-column label="产业分类" prop="industryName" width="220" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="行业领域" prop="domain" width="200" :show-overflow-tooltip="true"></el-table-column>

				<!--				<el-table-column label="行业领域" prop="aptitude" :show-overflow-tooltip="true" width="260">-->
<!--					<template #default="scope">-->
<!--						<div slot="content">-->
<!--								&lt;!&ndash; <div v-if="scope.row.industry_class !== undefined" >-->
<!--									<el-tag  type="primary" size="small" v-for="item in scope.row.industry_class.split(',')" :key="item">{{item}}</el-tag>-->
<!--								</div> &ndash;&gt;-->
<!--								<div v-if="scope.row.industry_class !== undefined" >-->
<!--									<div  v-for="(item,index) in scope.row.industry_class.split(',')" :key="item">-->
<!--										<el-tag v-if="index==0" type="primary" size="small">{{item}}</el-tag>-->
<!--										<el-tag v-else-if="index>0 && scope.row.show" type="primary" size="small">{{item}}</el-tag>-->
<!--										<el-button ref="myButton" v-if="index==0 &&scope.row.industry_class.split(',')[1]!==undefined" @click="doShowTabs($event,scope.row)" style=" padding: 3px 0" type="text" :class="more_btn">...</el-button>-->

<!--									</div>-->
<!--								</div>-->

<!--					    </div>-->
<!--					</template>-->
<!--				</el-table-column>-->
				<el-table-column label="收藏时间" prop="createTime" :show-overflow-tooltip="true" width="200"></el-table-column>
				<el-table-column label="操作" fixed="right" align="left" width="260">
					<template #default="scope">
						<el-button-group>
<!--							<el-button text type="primary" size="small" @click="to_profile(scope.row, scope.$index)" style="padding-left: 0;">查看画像</el-button>-->
							<el-button text type="primary" size="small" @click="setTarget(scope.row, scope.$index)">目标客户</el-button>
							<el-popconfirm title="确定取消收藏吗？" @confirm="changeCollect(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">取消收藏</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
	<finishDialog v-if="dialog.finish" ref="finishDialog" @success="handleSaveSuccess" @closed="dialog.finish=false"></finishDialog>
	<recordDetails v-if="dialog.detailsSave" ref="recordDialog" @closed="dialog.detailsSave=false"></recordDetails>
	<targetDialog v-if="dialog.targetSave" ref="targetDialog" @closed="dialog.targetSave=false"></targetDialog>
    <editDialog v-if="dialog.edit" ref="editDialog" @success="handleSaveSuccess" @closed="dialog.edit=false"></editDialog>
</template>

<script>

import scSelectFilter from '@/components/scSelectFilter'
import selectUserDialog from "@/views/module/selectUserDialog";
import saveDialog from './save'
import recordDetails from "./details";
import finishDialog from './finish'
import targetDialog from './target'

/**
 * 企业档案列表
 * @author yangfei
 */
export default {
    name: 'business archives',
	components: {
		saveDialog,
		scSelectFilter,
		selectUserDialog,
		finishDialog,
		recordDetails,
		targetDialog
	},
	data() {
		return {
			dialog: {
				save: false,
				detailsSave: false,
				finish:false
			},
			filterData: [
				{
					title: "收藏时间",
					key: "store_date",
					multiple: false,
					options: [{
                            label: "全部",
                            value: ""
                        },{
                            label: "本月",
                            value: "0"
                        }
						,{
                            label: "一个月",
                            value: "1"
                        }
						,{
                            label: "三个月",
                            value: "2"
                        }
						,{
                            label: "六个月",
                            value: "3"
                        }
						,{
                            label: "一年以内",
                            value: "4"
                        } ],
				},
				{
					title: "产业分类",
					key: "industry_classification",
					multiple: false,
					options: [
                            {
                            label: "全部",
                            value: ""
                        }
                    ]
				},
				//20240715去掉了，找不到对应的企业资质数据，后续确认后再添加
				// {
				// 	title: "企业资质",
				// 	key: "equalification",
				// 	multiple: false,
				// 	options: [{
				// 		value: '',
				// 		label: '全部'
				// 	}

					// ],
				// }

			],
			apiObj: this.$API.enterprise.company.getStoreCompaniesList,
			selection: [],
			search: {
				date: '',
				industry_classification:'', //产业分类
				equalification: '', //性质
				type: '',
				status: '',
				handedUser: '',
				keyword: null
			},
			more_tag_class: "tag_class0",
			icObjs:{},
			eqObjs:{}
        }
    }
    ,
	mounted() {
		this.getSearchConfigForSelect();
	},
	methods: {

		doShowTabs(val, row) {
		    this.more_tag_class=this.more_tag_class== 'tag_class1' ? 'tag_class0' : 'tag_class1'
			row.show = this.isValid(row.show) ? !row.show : true;
		},
		isValid(data){
			if(data==null){
				return false;
			}else{
				return true;
			}
		},
		openUrl(url) {
			window.open(url);
		},
		async getSearchConfigForSelect() {
			// var res = await this.$API.system.sysDic.getSysDicByCode.get({code:'FT-classifyType'});
			let res = await this.$API.enterprise.company.getInvestType.get();
			if (res.code == 200) {

				this.isObjs = res.data;
				let length = this.isObjs.length
				let  icList =[]
				for(var i=0;i<this.isObjs.length;i++){
					var jsObj = this.isObjs[length-1-i];
					var value = jsObj['label']
					var key = jsObj['value']
					this.icObjs[key]=value
					icList.push(jsObj)
				}
				this.filterData[1].options = this.filterData[1].options.concat(icList);

			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}

			//企业资质 qualificationType
			res = await this.$API.system.sysDic.getSysDicByCode.get({code:'qualificationType'});
			if (res.code == 200) {
				this.filterData[2].options = this.filterData[2].options.concat(res.data);

				this.isObjs = res.data;
				for(let i=0;i<this.isObjs.length;i++){
					let jsObj = this.isObjs[i];
					let value = jsObj['label']
					let key = jsObj['value']
					this.eqObjs[key]=value
				}
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}


		},
		//添加
		sendEvent() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.edit = true
			this.$nextTick(() => {
				//this.$refs.saveDialog.open('edit').setData(row)
				this.$refs.editDialog.open('edit').setData(row)
			})
		},
		setFinishStatus(row)
		{
			this.dialog.finish = true
			this.$nextTick(() => {
				this.$refs.finishDialog.open().setData(row);
			})
		},
		setTarget(row)
		{
			this.dialog.targetSave = true
			this.$nextTick(() => {
				this.$refs.targetDialog.open().setData(row);
			})
		},
		//查看
		table_show(row) {
			this.dialog.detailsSave = true
			this.$nextTick(() => {
				//this.getCompanyLogo(row);
				this.$refs.recordDialog.open().setData(row)
			})
		},

		//查看
		to_profile(row) {
			//alert(row.name)
			this.$router.push({path: '/enterprise/profile', query: {name: row.name}})
		},



		async getCompanyLogo(row){
			//let res = await this.$API.enterprise.company.getCompanyLogo.get({companyName:this.form.name});
			let res = await this.$API.enterprise.company.getCompanyLogo.get({companyName:"浙江大华技术股份有限公司"});
			if(res.status==200){
				row.logoUrl = res.data
				this.$refs.recordDialog.open().setData(row)
			}
		},
		//删除
		async table_del(row) {
			var res = await this.$API.enterprise.company.deletData.delete({id:row.id});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//取消收藏
		changeCollect(row) {
			//3.等待接口返回后改变值
			setTimeout(async() => {
				// var res = await this.$API.enterprise.company.collectEpData.post({id:row.ep_id,collect:0});
				// if (res.code == 200) {
				// 	//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				// 	this.$message.success("操作成功")
				// 	this.$refs.table.refresh()
				// } else {
				// 	this.$alert(res.message, "提示", {type: 'error'})
				// }
				var res = await this.$API.enterprise.adaptationAnalysis.updateCollectCompany.get(row);
				if (res.code === 200) {
					this.$message.success("取消收藏成功");
					this.$refs.table.refresh()
				} else {
					this.$alert(res.message, "提示", { type: "error" });
				}


			}, 500)
		},


		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		async upsearch() {

			//alert(this.search.keyword)
			this.$refs.table.upData(this.search)
		},
		filterChange(data) {
			if(data.industry_classification!=undefined){
				this.search.industry_classification=this.icObjs[data.industry_classification]; //产业分类
			}
			if(data.equalification!=undefined){
				this.search.equalification=this.eqObjs[data.equalification];//性质
			}
			this.search.total_score = data.total_score;
			// this.search.type = data.type;
			// this.search.status = data.status;
			this.search.industry_classification=data.industry_classification;
			this.search.keyword = data.keyword;
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		},
		async batchCancel(){
			let ids=[];
			let secData = this.selection;
			if (secData && secData.length>0){
				for(let index in secData){
					ids.push(secData[index].id);
				}
			}
			if (ids.length > 0){
				let res = await this.$API.enterprise.company.batchCancelCollect.post({ids:JSON.stringify(ids)});
				if (res.code === 200) {
					this.$message.success("取消收藏成功");
					this.$refs.table.refresh()
				} else {
					this.$alert(res.message, "提示", { type: "error" });
				}
			}
		}
	}
}

</script>

<style scoped lang="scss">
	::v-deep .tag_class0 {
		display: none
	}.tag_class1 {
		display:inline
	}
	.more_btn{
		font-size: 14px;

	}
  ::v-deep .el-popper {
	max-width: 40% !important;
	}
</style>
